package com.dai.code;

import java.util.Stack;

/**
 * @author daijiyong
 */
public class ValidParentheses {

    public static void main(String[] args) {
        String s = "(){}[]";
        System.out.println(isValid(s));
    }


    public static boolean isValid(String s) {
        if (null == s || s.length() == 0) {
            return false;
        }

        Stack<Character> stack = new Stack<>();
        for (Character c : s.toCharArray()) {
            if (c == '(' || c == '{' || c == '[') {
                stack.push(c);
            }
            if (c == ')') {
                if (stack.isEmpty() || stack.pop() != '(') {
                    return false;
                }
            }
            if (c == '}') {
                if (stack.isEmpty() || stack.pop() != '{') {
                    return false;
                }
            }
            if (c == ']') {
                if (stack.isEmpty() || stack.pop() != '[') {
                    return false;
                }
            }
        }
        return stack.isEmpty();
    }
}
